/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdCurrency.Handlers;

import com.SySammy.SdBountyHunter.sdLawCommandExport;
import com.SySammy.SdCurrency.Prop.CurrencyProperties;
import com.SySammy.SdCurrency.sdC;
import com.SySammy.SdCurrency.sdCCommandExport;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

/**
 *
 * @author Sammy
 */
public class LoanRun implements Runnable {

    private BankHandler BH = new BankHandler();
    private sdLawCommandExport CE = new sdLawCommandExport();
    private sdCCommandExport CC = new sdCCommandExport();
    private sdC plugin;
    private String s;

    public LoanRun(String s, sdC plugin) {
        this.s = s;
        this.plugin = plugin;
    }

    @Override
    public void run() {
        try {
            if (BH.GetloanQnt(s) <= 0) {
                BH.RemoveLoan(s);
                plugin.getServer().getScheduler().cancelTask(BH.GetloanIDRep(s));
                plugin.getServer().getPlayer(s).sendMessage(ChatColor.DARK_AQUA + "You have repayed your debt in time, have a nice day");
                return;
            } else {
                CE.MakeBounty(s, CurrencyProperties.Prisonowner, 1000, " Didn't repayed bank debt");
                plugin.getServer().broadcastMessage(ChatColor.DARK_AQUA + "A " + ChatColor.GOLD + CE.GetBounty(s) + " " + CC.GetCurrency() + ChatColor.DARK_AQUA + " bounty was started on " + ChatColor.GOLD + s + "'s " + ChatColor.DARK_AQUA + "head.");
                plugin.getServer().broadcastMessage(ChatColor.DARK_AQUA + "Reason: " + ChatColor.GOLD + CE.GetReason(s));
                plugin.getServer().getScheduler().cancelTask(BH.GetloanIDRep(s));
                if (CC.HasMoney(s, CC.GetloanRepaid(s)) == true) {
                    CC.TakeMoney(s, CC.GetloanRepaid(s));
                } else if (CC.GetMoney(s) < CC.GetloanRepaid(s)) {
                    CC.TakeMoney(s, CC.GetMoney(s));
                }
                Player ply = plugin.getServer().getPlayer(s);
                ply.sendMessage(ChatColor.RED + "====================== WARNING ======================\n");
                ply.sendMessage(ChatColor.RED + " You didn't repayed your debt, legal measures were applied.\n");
                ply.sendMessage(ChatColor.RED + "====================== WARNING ======================");
                return;
            }
        } catch (Exception e) {
        }
    }
}
